﻿//-----------------------------------------------------------------------
// <copyright file="TaggingEngine.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Yogesh M Joshi</author>
// <email>ymjoshi@live.com</email>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Application.Tagging
{
    using System;
    using System.IO;
    using Microsoft.SharePoint;
    using SmartTag.Application.CommandLine;

    /// <summary>
    /// Main class that tags all document libraries and required fields
    /// Options form command line arguments are directly passed to this engine.
    /// </summary>
    internal class TaggingEngine
    {
        #region "Private Fields"

        /// <summary>
        /// Url of the site
        /// </summary>
        private string siteUrl;

        /// <summary>
        /// Array of document library names
        /// </summary>
        private string[] docLibs;

        /// <summary>
        /// Array of column names
        /// </summary>
        private string[] columnNames;

        /// <summary>
        /// Number of worker threads
        /// </summary>
        private int threadCount;

        /// <summary>
        /// Number of words to scan in given document
        /// P.S. This is an approximate count
        /// </summary>
        private int wordsToScan;

        /// <summary>
        /// Relevance of the term
        /// </summary>
        private int relevance;

        #endregion

        /// <summary>
        /// Initializes a new instance of the TaggingEngine class
        /// </summary>
        /// <param name="siteUrl">Url of the site</param>
        /// <param name="docLibs">Array of document library names</param>
        /// <param name="columnNames">Array of column names</param>
        /// <param name="relevance">Relevance of the term</param>
        /// <param name="wordsToScan">Number of words to be scanned from document</param>
        /// <param name="threadCount">Number of worker threads</param>
        public TaggingEngine(
            string siteUrl,
            string[] docLibs,
            string[] columnNames,
            int relevance,
            int wordsToScan,
            int threadCount)
        {
            this.siteUrl = siteUrl;
            this.docLibs = docLibs;
            this.columnNames = columnNames;
            this.threadCount = threadCount;
            this.wordsToScan = wordsToScan;
            this.relevance = relevance;
            Logger.LogMessage("Initializing tagging engine...");
        }

        /// <summary>
        /// Start tagging operation
        /// </summary>
        public void Start()
        {
            try
            {
                using (SPSite site = new SPSite(this.siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        // Get document libraries
                        if (this.docLibs != null && this.docLibs.Length > 0)
                        {
                            foreach (string docLib in this.docLibs)
                            {
                                SPList list = null;
                                try
                                {
                                    list = web.GetList(docLib);
                                    DoclibTagger doclibTagger = new DoclibTagger(list, this.columnNames, this.wordsToScan, this.relevance, this.threadCount);

                                    doclibTagger.Start();
                                }
                                catch (FileNotFoundException ex)
                                {
                                    Logger.LogException("Document library not found!", ex);
                                }
                            }
                        }
                        else
                        {
                            Logger.LogMessage("No document libraries specified. Scanning all document libraries...");

                            // Get all document libraries
                            // Tag them
                            foreach (SPList list in web.Lists)
                            {
                                if (list is SPDocumentLibrary)
                                {
                                    DoclibTagger doclibTagger = new DoclibTagger(list, this.columnNames, this.wordsToScan, this.relevance, this.threadCount);

                                    doclibTagger.Start();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException("Error occured.", ex);
            }
        }
    }
}
